﻿using ConfExpert.Utility;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class BeklenenYuklemeler : XPopupForm
    {
        public BeklenenYuklemeler()
        {
            InitializeComponent();
        }

        private void BeklenenYuklemeler_Load(object sender, EventArgs e)
        {
            //ShowOnlyFooter();
            FooterText = "Stok girişini yapacağınız yüklemeye çift tıklayın.";

            gleBelgeTipi.Edit.Assign(RI(Rp.BelgeTipiAlis));
            gleVendor.Edit.Assign(RI(Rp.V_VendorK));
            ((RepositoryItemGridLookUpEdit)gleBelgeTipi.Edit).TextEditStyle =
            ((RepositoryItemGridLookUpEdit)gleVendor.Edit).TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;

            //gv.SetViewCaption("Gelmesi beklenen yüklemeler");

            btnGo.Enabled = false;
            gleVendor.EditValueChanged += GleVendor_EditValueChanged;
            gleBelgeTipi.EditValueChanged += gleBelgeTipi_EditValueChanged;

            txtBelgeNo.EditValueChanged += txtBelgeNo_EditValueChanged;
            btnGo.ItemClick += BtnGo_ItemClick;

            this.Shown += StokGirisKst_Shown;
            btnGo.Visibility = gleBelgeTipi.Visibility = gleVendor.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            Rfrsh();
        }

        void GoEnabled()
        {
            btnGo.Enabled = gleVendor.EditValue.ToInt() > 0 && txtBelgeNo.EditValue.ToStr() != string.Empty && gleBelgeTipi.EditValue.ToInt() > 0;
        }

        private void GleVendor_EditValueChanged(object sender, EventArgs e)
        {
            GoEnabled();
        }

        void StokGirisKst_Shown(object sender, EventArgs e)
        {
            //((RepositoryItemTextEdit)txtBelgeNo.Edit).Focus();
        }

        void txtBelgeNo_EditValueChanged(object sender, EventArgs e)
        {
            GoEnabled();
            Rfrsh();
        }

        void gleBelgeTipi_EditValueChanged(object sender, EventArgs e)
        {
            GoEnabled();
        }

        void Rfrsh()
        {
            gv.Init("GetBeklenenYuklemeler2", txtBelgeNo.EditValue.ToStr(), isKD);
            //gv.SetCaptions("YuklemeId:Yükleme Id", "IrsaliyeNo:İrsaliye No", "YuklemeTarihi:Yükleme Tarihi", "QtyYuklenen:Yüklenen MT");
            gv.MakeReadOnly();

            gv.MakeRowSelectable();
            gv.MakeColumnInvisible("idxs", "YuklemeId");
            gv.FormatAsPercentage("Score");
            gv.FormatAsDate("YuklemeTarihi", "ETA");
            gv.FormatAsNumber(1, "TotalQty");
            gv.AddSummary("TotalQty");
            gv.BestFitColumns();
        }

        private void BtnGo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Exists())
                return;

            AppSession.MainForm.ShowMdiChildForm(typeof(ConfExpert.Forms.Depo.StokGiris), gleVendor.EditValue.ToInt(), txtBelgeNo.EditValue.ToStr(), gleBelgeTipi.EditValue.ToInt());
            this.DialogResult = DialogResult.OK;
        }

        bool Exists()
        {
            int YuklemeId = gv.GetFocusedDataRow()["YuklemeId"].ToInt();
            if (ExecuteScalar("CheckYuklemeId", YuklemeId).ToInt() > 0)
            {
                ShowErrorMessage("Bu yüklemenin girişi daha önce yapılmış.");
                return true;
            }

            return false;
        }

        private void gv_DoubleClick(object sender, EventArgs e)
        {
            Point pt = gv.GridControl.PointToClient(Control.MousePosition);
            GridHitInfo info = gv.CalcHitInfo(pt);

            if (info.RowHandle > -1)
                if (info.InRow || info.InRowCell)
                {
                    int YuklemeId = gv.GetDataRow(info.RowHandle)["YuklemeId"].ToInt(-1);

                    if (Exists())
                        return;

                    if (isKD)
                        AppSession.MainForm.ShowMdiChildForm(typeof(ConfExpert.Forms.SatinAlma.YeniYapi.StokGirisKD), YuklemeId);
                    else
                        AppSession.MainForm.ShowMdiChildForm(typeof(ConfExpert.Forms.SatinAlma.YeniYapi.StokGiris), YuklemeId);

                    this.DialogResult = DialogResult.OK;

                }

        }
    }
}